import os
from shutil import copyfile
from loguru import logger


def print_files(path):
    target_prefix = 'N:\\手机视频\\filter_image\\'
    lsdir = os.listdir(path)
    dirs = []
    for i in lsdir:
        sub_path = os.path.join(path, i)
        if os.path.isdir(sub_path):
            dirs.append(i)
    if dirs:
        for j in dirs:
            next_dir = os.path.join(path, j)
            print_files(next_dir)
    files = []
    for k in lsdir:
        sub_file = os.path.join(path, k)
        if os.path.isfile(sub_file):
            files.append(k)
    for f in files:
        file_path = os.path.join(path, f)
        if file_path.lower().endswith('jpg') or file_path.lower().endswith('jpg') or file_path.lower().endswith(
                'jpeg') or file_path.lower().endswith('png') or file_path.lower().endswith('bmp'):
            logger.info('开始复制当前文件：{current_file}', current_file=file_path)
            # 复制文件
            target_file = target_prefix + f
            copyfile(file_path, target_file)
        else:
            # 删除文件
            logger.warning('开始删除当前文件：{delete_file}', delete_file=file_path)
            os.remove(file_path)


logger.add("get_file_by_single.log", format="{time} {level} {message}", filter="", level="INFO")
logger.info('开始遍历目录，去寻找image图片，并复制到新目录')
image_path = 'N:\\手机视频\\image2'
print_files(image_path)
